package huxibianjie.com.lbscandroid.ui.new_activity;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import huxibianjie.com.lbscandroid.R;
import huxibianjie.com.lbscandroid.base.BaseActivity;
import huxibianjie.com.lbscandroid.event.ClickEvent;
import huxibianjie.com.lbscandroid.manager.RouteManager;
import huxibianjie.com.lbscandroid.manager.UserManager;
import huxibianjie.com.lbscandroid.model.UserModel;
import huxibianjie.com.lbscandroid.task.Callback;
import huxibianjie.com.lbscandroid.task.Task;
import huxibianjie.com.lbscandroid.ui.Service;
import huxibianjie.com.lbscandroid.util.StringUtils;

/**
 * 提现界面
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvLbscNumber, tvPurseAddress, tvWithdraw;
    private LinearLayout layPurseAddress;

    @Override
    protected int getContentView() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void initView() {
        initTitle(R.drawable.ic_back, "货币提现");
        layPurseAddress = findViewById(R.id.lay_purse_address);
        tvLbscNumber = findViewById(R.id.tv_lbsc_number);
        tvPurseAddress = findViewById(R.id.tv_purse_address);
        tvWithdraw = findViewById(R.id.tv_withdraw);
    }

    private UserModel userModel;

    @Override
    protected void initData() {
        if (UserManager.getInstance().isLogin()) {
            userModel = UserManager.getInstance().getUserModel();
            tvLbscNumber.setText(String.valueOf(userModel.getMoney()));
            addressState();
        }
    }

    private void addressState() {
        if (userModel == null) return;
        String purseAddress = userModel.getPurseAddress();
        if (StringUtils.isEmpty(purseAddress)) {
            tvPurseAddress.setText("未绑定钱包地址");
        } else {
            tvPurseAddress.setText(String.valueOf("*****" + purseAddress.substring(36)));
        }
    }

    @Override
    protected void initListener() {
        tvWithdraw.setOnClickListener(this);
        layPurseAddress.setOnClickListener(this);
    }

    @Override
    public void onClickEvent(ClickEvent event) {
        super.onClickEvent(event);
        switch (event.type) {
            case ADDRESS_UPDATE_SUCCESS:
                initData();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_withdraw:
                if (!(userModel != null && userModel.getPurseAddress().length() == 42)) {
                    showToast("钱包地址错误，请重新绑定");
                    return;
                }
                if (Double.valueOf("5000.0") <= userModel.getMoney()) {
                    showLoading();
                    Task.create(Service.class).savePurseAddress(String.valueOf(userModel.getId()), userModel.getPurseAddress(),
                            String.valueOf(userModel.getMoney())).enqueue(new Callback<Map<String, String>>() {
                        @Override
                        public void onSuccess(@Nullable Map<String, String> mapData) {
                            cancelLoading();
                            Map<String, String> map = new HashMap();
                            map.put("phone", userModel.getPhone());
                            map.put("money", String.valueOf(userModel.getMoney()));
                            MobclickAgent.onEvent(WithdrawActivity.this, "Put_forward", map);
                            UserManager.getInstance().save(UserManager.getInstance().getUserModel().setMoney(0),
                                    false);
                            EventBus.getDefault().post(new ClickEvent(ClickEvent.Type.REFRESH_USER_INFO_SUCCESS, null, ""));
                            initData();
                            new android.support.v7.app.AlertDialog.Builder(WithdrawActivity.this).setMessage("提现成功")
                                    .setPositiveButton("确定", null).show();
                        }

                        @Override
                        public void onFailure(@NonNull String error) {
                            cancelLoading();
                            new android.support.v7.app.AlertDialog.Builder(WithdrawActivity.this).setMessage(error)
                                    .setPositiveButton("确定", null).show();
                        }
                    });
                } else {
                    showToast("提现币数最少为5000LBSC");
                }
                break;
            case R.id.lay_purse_address:
                RouteManager.getInstance().showUpdateWalletAddressActivity(this);
                break;
        }
    }
}
